package x.y.z;

public class LeetCode_0203 {
    public ListNode removeElementsRecur(ListNode head, int val) {
        if (head == null) {
            return head;
        }
        head.next = removeElementsRecur(head.next, val);
        return head.val == val ? head.next : head;
    }

    public ListNode removeElementsIter(ListNode head, int val) {
        if (head == null) {
            return head;
        }
        ListNode dummyNode = new ListNode();
        dummyNode.next = head;
        ListNode prev = dummyNode;
        while (head != null) {
            if (head.val == val) {
                head = head.next;
                prev.next = head;
            } else {
                prev.next = head;
                prev = head;
                head = head.next;
            }
        }
        return dummyNode.next;
    }
}
